<?php

///////////////////////////////////////////////////
//
// Basic Functions
// 
// If you want to write basic function, please here.
// Others, put them on above
//
//
///////////////////////////////////////////////////

/**
 * 对于页面 brand/xxx页面，得到当前页面的 tid 和 nid
 */
function best_common_page_brand_id($tid_only = false) {
  static $tid, $nid;

  if (!$tid || !$nid) {
    $brand_name = arg(1);

    if (!is_numeric($brand_name)) {
      $sql = "SELECT nid FROM content_type_brand_term WHERE field_brand_term_en_value = '%s'";
      $nid = db_result(db_query($sql, $brand_name));
    }
    else {
      $nid = $brand_name;
    }

    $sql = 'SELECT tid FROM taxonomynode WHERE nid = %d';
    $tid = db_result(db_query($sql, $nid));
  }

  if ($tid_only) {
    return $tid;
  }
  return array($tid, $nid);
  
}
/**
 * 对于页面 list/* 得到当前父类的tid
 */
function best_common_get_list_parent_tid() {
  $id = arg(1);
  if (is_numeric($id)) {
    $parents = taxonomy_get_parents($id);
    if(count($parents)) {
      $p = array_pop($parents);
      return $p->tid;
    }
  }
  return 0;
}

/**
 * 从taxonomy数组里面（如node->taxonomy）中得到指定vid的terms
 * @param type $taxonomy
 * @param type $vid
 * @return type 
 */
function best_get_terms_byvid($taxonomy, $vid) {
  $array = array();
  foreach ($taxonomy as $key => $val) {
    if($val->vid == $vid) {
      $array[$val->tid] = $val;
    }
  }
  return $array;
}
